package com.qhw.jdbc;

import java.sql.*;
import java.util.Scanner;

public class AppLogin {
    public static void main(String[] args) throws Exception{
        System.out.println("请输入账号：");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请输入密码：");
        String password = scanner.nextLine();
        login(name,password);
        //login1(name,password);
    }

    public static void login(String username ,String password) throws Exception {
        System.out.println("账号："+username);
        System.out.println("密码："+password);
        String sql = "select id from `user` where username = '" +username+
                "' and `password` = MD5(" +password+
                ")";
        System.out.println("组成的sql语句是"+sql);
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
        String name = "root";
        String password1 = "password";
        Connection connection = DriverManager.getConnection(url, name, password1);
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        if (resultSet.next())
            System.out.println("登录成功！");
        else System.out.println("登录失败");

    }
    public static void login1(String username ,String password) throws Exception {
        System.out.println("账号："+username);
        System.out.println("密码："+password);
        String sql = "select id from `user` where username = ? and `password` = MD5(?)";
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
        String name = "root";
        String password1 = "password";
        Connection connection = DriverManager.getConnection(url, name, password1);
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,username);
        preparedStatement.setString(2,password);

        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next())
            System.out.println("登录成功！");
        else System.out.println("登录失败");
    }
}
